package com.kapo.egroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kapo.egroup.entity.base.project.Project;
import com.kapo.egroup.entity.base.project.UserProjectRecord;
import com.kapo.egroup.entity.base.user.User;
import com.kapo.egroup.exception.ServiceException;
import com.kapo.egroup.mapper.ProjectMapper;
import com.kapo.egroup.mapper.UserMapper;
import com.kapo.egroup.mapper.UserProjectRecordMapper;
import com.kapo.egroup.service.ProjectService;
import com.kapo.egroup.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title UserServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2022-10-11 20:50
 **/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    UserProjectRecordMapper userProjectRecordMapper;

    @Override
    public List<Project> getMyProjects(Long userId) {
        return projectMapper.getMyProjectList(userId);
    }

    @Override
    public Project getProjectById(Long id) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("_id","leader_id","project_name","description","requirement","type","start_time","end_time","lesson_task_id").eq("_id",id);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateProject(Project projectReq) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_id",projectReq.getId());
        baseMapper.update(projectReq, queryWrapper);
    }

    @Override
    public List<Project> getProjectByType(Long type) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("_id","leader_id","project_name","description","requirement","type","start_time","end_time","lesson_task_id").eq("type",type);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Project> getProjectByLeaderId(Long leaderId) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("_id","leader_id","project_name","description","requirement","type","start_time","end_time","lesson_task_id").eq("leader_id",leaderId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Project> getProjectByLessonTaskId(Long lessonTaskId) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("_id","leader_id","project_name","description","requirement","type","start_time","end_time","lesson_task_id").eq("lesson_task_id",lessonTaskId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserProjectRecord> getMemberById(Long projectId) {
        QueryWrapper<UserProjectRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId);
        return userProjectRecordMapper.selectList(queryWrapper);
    }

    @Override
    public void createUserProjectRecord(Long leaderId,Long projectId) {
        UserProjectRecord userProjectRecord = new UserProjectRecord();
        userProjectRecord.setProjectId(projectId);
        userProjectRecord.setUserId(leaderId);
        userProjectRecordMapper.insert(userProjectRecord);
    }
}
